<template>
	<view>
		<uni-file-picker ref="fileUpload" v-model="files" :limit="limit" :readonly="readonly" mode="grid"  :title="title" @select="fileUploadSelect" :auto-upload="false" @delete="deleteImg"></uni-file-picker>
	</view>
</template>

<script>
	import  config from '@/config'
	import { getToken } from '@/utils/auth'
	export default {
		name:"common-attach",
		props:{
			limit:{
				type:Number,
				default:9
			},
			readonly:{
				type:Boolean,
				default:false
			},
			title:{
				type:String,
				default:"最多选择9张图片"
			},
			files:{
				type:Array
			}
		},
		data() {
			return {
				filePaths:[],
			};
		},
		methods:{
			fileUploadSelect(e){
				for (let i = 0; i < e.tempFilePaths.length; i++) {
					this.filePaths.push(e.tempFilePaths[i]);
				}
			},
			deleteImg(e) {
				for (let i = 0; i < this.filePaths.length; i++) {
					if (this.filePaths[i] == e.tempFilePath) {
						this.filePaths.splice(i, 1);
					}
				}	
			},
			isUpload(){
				return this.filePaths.length>0;
			},
			fileUpload(bussType,mainId){
				return new Promise((resolve,reject) => {
					this.filePaths.forEach(file => {
						uni.uploadFile({
							url: config.baseUrl+'/am/amAttach/uploadAndSaveTheFile', //仅为示例，非真实的接口地址
							filePath: file,
							name: 'file',
							header:{
								"Authorization": "Bearer " + getToken() 
							},
							formData: {
								bussType:bussType,
								mainId:mainId
							},
							success: (uploadFileRes) => {
								resolve()
							},
							fail() {
								reject()
							}
						});
					})
				})
			}
		}
	}
</script>

<style>

</style>